//
//  ViewController.swift
//  RFAds
//
//  Created by Roffa on 02/06/2023.
//  Copyright (c) 2023 Roffa. All rights reserved.
//

import UIKit
import RFAds
import GoogleMobileAds

//actor处理异步安全
@available(iOS 13.0.0, *)
actor aaa{
    @MainActor func onlyMainThread() {  //@MainActor标识后，只会在主线程运行
        var name: String? = "可选"
        
        if let name {
            print("新的阴影变量\(name)")
        }
        
    }
    func newSleep() async{
        if #available(iOS 16.0, *) {
            try? await Task.sleep(until: .now + .seconds(1), clock: .continuous)
        } else {
            // Fallback on earlier versions
        }
    }
}

class ViewController: UIViewController {
    @IBOutlet weak var bannerView: UIView!
    lazy var banner: RFAdBanner = {
        let banner = RFAdBanner()
        banner.rootVc = self

        bannerView.addSubview(banner.adBannerView)
        banner.adBannerView.backgroundColor = .red
        banner.adBannerView.translatesAutoresizingMaskIntoConstraints = false
        banner.adBannerView.leadingAnchor.constraint(equalTo: self.bannerView.leadingAnchor).isActive = true
        banner.adBannerView.trailingAnchor.constraint(equalTo: self.bannerView.trailingAnchor).isActive = true
        banner.adBannerView.bottomAnchor.constraint(equalTo: self.bannerView.bottomAnchor).isActive = true
        return banner
    }()
    
//    var bv: BannerView! = BannerView(adSize: AdSizeBanner)

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        bv = BannerView(adSize: AdSizeBanner)
//
//        addBannerViewToView(bv)
//        bv.rootViewController = self
//        bv.load(Request())
        
//        bannerView.addSubview(bv)
        
        tryShowBannerAd()

    }
    func tryShowBannerAd(){
//        RFAdBanner.unitId = "ca-app-pub-7611172625562343/5841751940"
//测试使用
        loadBannerAds()
    }
    
    func loadBannerAds(){
        
        banner.loadBanner()
        
    }

    
    override func viewDidAppear(_ animated: Bool) {
//        bv.adUnitID = "ca-app-pub-3940256099942544/2934735716"
//        bv.rootViewController = self
//        loadBannerAd()
       
        /*
        let banner = RFAdBanner()
        banner.rootVc = self
        banner.loadBanner()
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(2000)) {
            // your function here
            self.bannerView.addSubview(banner.adBannerView)

//            banner.adBannerView.backgroundColor = .systemPink
            banner.adBannerView.translatesAutoresizingMaskIntoConstraints = false
            banner.adBannerView.leadingAnchor.constraint(equalTo: self.bannerView.leadingAnchor).isActive = true
            banner.adBannerView.trailingAnchor.constraint(equalTo: self.bannerView.trailingAnchor).isActive = true
            banner.adBannerView.bottomAnchor.constraint(equalTo: self.bannerView.bottomAnchor).isActive = true
        }

        */
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   /*
    override func viewWillTransition(to size: CGSize,
                            with coordinator: UIViewControllerTransitionCoordinator) {
      super.viewWillTransition(to:size, with:coordinator)
      coordinator.animate(alongsideTransition: { _ in
        self.loadBannerAd()
      })
    }

    func loadBannerAd() {
      // Step 2 - Determine the view width to use for the ad width.
      let frame = { () -> CGRect in
        // Here safe area is taken into account, hence the view frame is used
        // after the view has been laid out.
        if #available(iOS 11.0, *) {
          return view.frame.inset(by: view.safeAreaInsets)
        } else {
          return view.frame
        }
      }()
      let viewWidth = frame.size.width

      // Step 3 - Get Adaptive AdSize and set the ad view.
      // Here the current interface orientation is used. If the ad is being preloaded
      // for a future orientation change or different orientation, the function for the
      // relevant orientation should be used.
        bv.adSize = CurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth)

      // Step 4 - Create an ad request and load the adaptive banner ad.
        bv.load(Request())
    }
  
*/

    
    func addBannerViewToView(_ bannerView: BannerView) {
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bannerView)
        view.addConstraints(
            [NSLayoutConstraint(item: bannerView,
                                attribute: .bottom,
                                relatedBy: .equal,
                                toItem: view.safeAreaLayoutGuide,
                                attribute: .top,
                                multiplier: 1,
                                constant: 0),
             NSLayoutConstraint(item: bannerView,
                                attribute: .centerX,
                                relatedBy: .equal,
                                toItem: view,
                                attribute: .centerX,
                                multiplier: 1,
                                constant: 0)
            ])
        
    }
     
}

